import store from "store";
let defaultBizMap = {
	company: [],
	brand: [],
	city: [],
	shop: [],
	shop_childs: [],
	region: [],
	level: []
}
const state = {
	business: store.get("business") || [],
	bizList: store.get("bizList") || [],
	bizMap: store.get("bizMap") || {},
	bizAll: store.get("bizAll") || []
}
const actions = {
	async getBusinessAll({ commit }) {
		return this.$service.business.all().then((data) => {
			commit("SET_BUSINESS_ALL", data);
			return data;
		})
	},
	/**
	 * 获取事业部
	 */
	async getBusiness({ commit }, has_headquarters = 0) {
		return this.$service.public.business({has_headquarters}).then(data => {
			let list = (data || []).map(v => ({ ...v, label: v.biz_name, value: v.biz_id }))
			// .sort((a, b) => a.addtime - b.addtime);
			if(has_headquarters == 1){
				return list
			}
			commit("SET_BUSINESS", list);
			return list;
		});
	},
	// 获取分公司
	async getCompany({ commit }, biz_id) {
		return this.$service.company.list({ biz_id }).then(data => {
			let list = (data || []).map(v => ({ ...v, label: v.company_name, value: v.company_id }));
			commit("SET_BIZ_MAP", { biz_id, key: 'company', list });
			return list;
		})
	},
	// 获取品牌
	async getBrand({ commit }, biz_id) {
		return this.$service.brand.usermanage({ biz_id }).then(data => {
			let list = (data || []).map(v => ({ ...v, label: v.bra_name, value: v.bra_id }));
			commit("SET_BIZ_MAP", { biz_id, key: 'brand', list });
			return list;
		})
	},
	// 获取城市
	async getCity({ commit }, biz_id) {
		return this.$service.spread.getbizcity({ biz_id }).then(data => {
			let list = (data || []).map(v => ({ ...v, label: v.name, value: v.id }));
			commit("SET_BIZ_MAP", { biz_id, key: 'city', list });
			return list;
		})
	},
	// 获取门店
	async getShop({ commit }, biz_id) {
		return this.$service.shop.list({ biz_id }).then(data => {
			let list = (data || []).reduce((prev, curr) => [...prev, ...curr.children], []);
			commit("SET_BIZ_MAP", { biz_id, key: 'shop', list: data });
			commit("SET_BIZ_MAP", { biz_id, key: 'shop_childs', list });
			return data || [];
		})
	},
	// 获取区域
	async getRegion({ commit }, biz_id) {
		return this.$service.region.list({ biz_id }).then(data => {
			let list = (data || []).map(v => ({ ...v, label: v.name, value: v.id }));
			commit("SET_BIZ_MAP", { biz_id, key: 'region', list });
			return list;
		})
	},
	// 会员等级
	async getLevel({ commit }, biz_id) {
		return this.$service.public.cusLevel({ biz_id }).then(data => {
			let list = (data || []).map(v => ({ ...v, label: v.cl_name, value: v.cl_id }));
			commit("SET_BIZ_MAP", { biz_id, key: 'level', list });
			return list;
		})
	},
	/**
		 * 获取部门
		 * @param params biz_id 事业部id, needAll 是否在数组到插入-全部
		 */
	async getDept({ commit }, params) {
		const { cascader = false, ...args } = params;
		return this.$service.dept.list({ parent_id: 0, is_store: 0, ...args }).then(data => {
			let list = data || [];
			if (!cascader) {
				list = (data || []).reduce((prev, curr) => [...prev, ...delEmptyChildren(curr.children)], []);

			}
			return list;
		});
	}
}
const mutations = {
	ALL_RESET(state) {
		state.business = [];
		state.bizList = [];
		state.bizMap = {}
		store.remove("business");
		store.remove("bizList");
		store.remove("bizMap");
	},
	SET_BUSINESS_ALL(state, list) {
		state.bizAll = list
		store.set("bizAll", list);
	},
	SET_BUSINESS(state, list) {
		let bizList = list.filter(v => v.biz_id != 1);
		state.business = bizList;
		state.bizList = list;
		let bizMapTemp = state.bizMap
		let bizMap = list.reduce((prev, curr) => {
			let key = 'biz' + curr.biz_id
			return {
				...prev,
				[key]: {
					biz_id: curr.biz_id,
					...defaultBizMap,
					...bizMapTemp[key]
				}
			}
		}, {})
		bizMap.biz0 = {
			...defaultBizMap,
			...bizMapTemp.biz0
		}
		state.bizMap = bizMap;
		store.set("bizMap", bizMap);
		store.set("business", bizList);
		store.set("bizList", list);
	},
	SET_BIZ_MAP(state, data) {
		let bizKey = 'biz' + data.biz_id
		let biz = state.bizMap[bizKey]
		if (!biz) {
			state.bizMap[bizKey] = {
				biz_id: data.biz_id,
				...defaultBizMap
			}
		}
		state.bizMap[bizKey][data.key] = data.list || []
		// Vue.set(state.bizMap['biz' + data.biz_id], data.key, data.list)
		store.set("bizMap", state.bizMap);
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
